package com.lwf.smartseniorcare.config;

import com.lwf.smartseniorcare.entity.User;
import com.lwf.smartseniorcare.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) throws Exception {
        // Create admin user if not exists
        if (!userRepository.existsByUsername("admin")) {
            User admin = new User();
            admin.setUsername("admin");
            admin.setPassword(passwordEncoder.encode("admin123"));
            admin.setEmail("admin@example.com");
            admin.setPhone("1234567890");
            admin.setRole("ADMIN");
            admin.setEnabled(true);
            userRepository.save(admin);
            System.out.println("Admin user created successfully");
        }

        // Create a sample doctor user
        if (!userRepository.existsByUsername("doctor")) {
            User doctor = new User();
            doctor.setUsername("doctor");
            doctor.setPassword(passwordEncoder.encode("doctor123"));
            doctor.setEmail("doctor@example.com");
            doctor.setPhone("1234567891");
            doctor.setRole("DOCTOR");
            doctor.setEnabled(true);
            userRepository.save(doctor);
            System.out.println("Doctor user created successfully");
        }

        // Create a sample caretaker user
        if (!userRepository.existsByUsername("caretaker")) {
            User caretaker = new User();
            caretaker.setUsername("caretaker");
            caretaker.setPassword(passwordEncoder.encode("caretaker123"));
            caretaker.setEmail("caretaker@example.com");
            caretaker.setPhone("1234567892");
            caretaker.setRole("CARETAKER");
            caretaker.setEnabled(true);
            userRepository.save(caretaker);
            System.out.println("Caretaker user created successfully");
        }
    }
}
